package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/26 11:22
 * @Version 1.0
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;

    /**
     * 制作mapping
     * @return
     */
    @GetMapping("inner/createIndex")
    public Result createIndex(){
        //  创建索引 -- 后续让项目启动的时候，就创建索引库. 6.8
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        //  上架
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        //  下架
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    /**
     * 根据skuId 做热度排名
     * @param skuId
     * @return
     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId){
        //  调用服务层方法
        this.searchService.incrHotScore(skuId);
        return Result.ok();
    }

    /**
     * 检索数据  api/list
     * @param searchParam
     * @return
     */
    @PostMapping
    public Result search(@RequestBody SearchParam searchParam){
        //  调用服务层方法
        SearchResponseVo searchResponseVo = searchService.search(searchParam);
        return Result.ok(searchResponseVo);
    }
}
